package com.heima.controller;

import com.heima.common.result.PageResult;
import com.heima.common.result.Result;
import com.heima.pojo.dto.UserDTO;
import com.heima.pojo.dto.UserPageQuery;
import com.heima.pojo.entity.User;
import com.heima.pojo.vo.UserInfoVO;
import com.heima.pojo.vo.UserVO;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * \* User: chy
 * \* Date: 2025/8/22  20:41
 * \* Description:
 * \
 */
@RestController
@RequestMapping("/api/user-service/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询人员列表
     * @param userDTO
     * @return
     */
    @GetMapping("/search")
    public PageResult<UserVO> search(UserPageQuery userDTO) {
        PageResult<UserVO> userVOPageResult = userService.pageResult(userDTO);
        return userVOPageResult;
    }

    /**
     * 新增人员
     * @param user
     * @return
     */
    @PostMapping
    public Result save(@RequestBody User user) {
        boolean save = userService.save(user);
            return Result.success( save);
    }

    /**
     * 修改人员
     * @param userId
     * @param user
     * @return
     */
    @PutMapping("/{userId}")
    public Result update(@PathVariable Integer userId, @RequestBody User user) {
        log.info("修改人员数据:{}", user);
        user.setId(userId);
        boolean update = userService.updateById(user);
        return Result.success(update);
    }

    /**
     * 删除人员
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public Result delete(@PathVariable Integer userId) {
        log.info("删除人员数据:{}", userId);
        boolean delete = userService.removeById(userId);
        return Result.success(delete);
    }

    /**
     * 根据id查询人员信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public UserInfoVO getById(@PathVariable Integer id) {
        log.info("根据用户id获取用户信息:{}", id);
        UserInfoVO userInfoVO = userService.getVoById( id);
        return userInfoVO;
    }
}
